package edu.pxd.cecs.ece510.pocketmanager;

import java.util.ArrayList;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.view.Menu;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;

public class ExpensesActivity extends Activity {
	private ListView listname;
	private Button addbutton;
	private Button mContinueButton;
	private int numberofitems;
	private ArrayList<String> nameArr;
	private EditText addName;
	private ArrayAdapter<String> adapter;
	private EditText eventname;
	private String EventTable = "EventTable";
	public SQLiteDatabase db1;
	private String PATH = "/data/data/edu.pxd.cecs.ece510.pocketmanager/databases/";
	private Boolean flag;
	private Boolean blank_flag;
	private int arrsize;
	private String refer_table_name;

	
	
	String[] set;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_expens);
				
		addbutton = (Button)findViewById(R.id.AddButton);
		addName = (EditText)findViewById(R.id.NewNameTextField);
		listname=(ListView)findViewById(R.id.listView10);
		nameArr = new ArrayList<String>();		
		adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1 ,nameArr);
		listname.setAdapter(adapter);
		eventname = (EditText)findViewById(R.id.EventNameTextField);
		
		flag = CreateActivity.db.isOpen();
		db1 = SQLiteDatabase.openOrCreateDatabase(PATH + "EventDatabase", null);
		
		//db1.execSQL("DROP TABLE IF EXISTS" + EventTable);
		//db1.execSQL("CREATE TABLE IF NOT EXISTS EventTable(eventname VARCHAR, id INTEGER);");
		//DataBase db = new DataBase(this);		
		//db1.execSQL("DROP TABLE IF EXISTS'" + EventTable + "'");
		
		//db1.execSQL("CREATE TABLE IF NOT EXISTS'" + EventTable + "'(eventname VARCHAR, id INTEGER PRIMARY KEY AUTOINCREMENT);");
				
		addbutton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String uName = addName.getText().toString();
				if(uName.matches(""));
				else{
				nameArr.add(addName.getText().toString());
				adapter.notifyDataSetChanged();
				addName.setText("");
				}
			}			
		});
		
		listname.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				nameArr.remove(arg2);
				adapter.notifyDataSetChanged();
				return false;
			}
		});
		
		mContinueButton = (Button)findViewById(R.id.continuebutton);
	    mContinueButton.setOnClickListener(new View.OnClickListener() {
	    
	    @Override
		public void onClick(View v) {
	    	
	    	if(!(eventname.getText().toString().matches(""))){
	    	//CallCreateDataBase();
	    	//final String S = "INSERT INTO EventTable VALUES('" + eventname.getText().toString() + "', '1');";
	    	
	    	final String S = "INSERT INTO EventTable VALUES('" + eventname.getText().toString() + "', NULL);";
	    	
			//CreateActivity.db.execSQL("CREATE TABLE IF NOT EXISTS'" + EventTable + "'(eventname VARCHAR, id INTEGER PRIMARY KEY AUTOINCREMENT);");    	
			CreateActivity.db.execSQL(S);
			Cursor c = CreateActivity.db.rawQuery("SELECT * from EventTable", null);
			c.moveToLast();
	    	
			//String tableId = String.valueOf(c.getInt(c.getColumnIndex("id")));
			//Log.d("ID", tableId);
			refer_table_name = eventname.getText().toString().replaceAll(" ", "");
			String tableName = c.getString(c.getColumnIndex("eventname"));
			Log.d("TableName", tableName);
			CreateActivity.db.execSQL("CREATE TABLE IF NOT EXISTS'" + refer_table_name + "'(personeName VARCHAR, amountPay FLOAT, calculatedAmount FLOAT);");
			arrsize = nameArr.size();
			for(int i= 0; i< arrsize; i++)
			{
				String persone = nameArr.get(i);
				Log.d("NAME", persone);
				final String newValue = "INSERT INTO '" + refer_table_name + "' VALUES('" + persone + "', NULL, NULL);";
				CreateActivity.db.execSQL(newValue);
			}
			//final String newValue = "INSERT INTO '" + tableName + "' VALUES('xyz','500', '20');";
			//CreateActivity.db.execSQL(newValue);
			
				Cursor idTable = CreateActivity.db.rawQuery("SELECT * from " + refer_table_name, null);
				int count1 = idTable.getCount();
				idTable.moveToFirst();
				for(int track1 = 0;track1 < count1; track1++)
				{
					Log.d("NAME", idTable.getString(idTable.getColumnIndex("personeName")));
					Log.d("amount", String.valueOf(idTable.getFloat(idTable.getColumnIndex("amountPay"))));
					Log.d("pay", String.valueOf(idTable.getInt(idTable.getColumnIndex("calculatedAmount"))));
					idTable.moveToNext();
				}
				
			

	    	
			//SQLiteDatabase.this.execSQL(S);
	    	//Cursor c = db.rawQuery("SELECT * from EventTable", null);
	    	//c.moveToFirst();
	    	//c.moveToFirst();
	    	//c.moveToLast();
	    	//int count =  c.getCount();
//	    	Log.d("count", String.valueOf(count));
//	    	for(int i = 0; i< count; i++){
//	    		if(i == 0){
//	    			c.moveToFirst();
//	    		}
//	    		else{
//	    			c.moveToNext();
//	    		}
//	    			
//	    		Log.d("EVENTNAME", c.getString(c.getColumnIndex("eventname")));
//		    	Log.d("ID", String.valueOf(c.getInt(c.getColumnIndex("id"))));
//		    	
//	    	}
	    	
			//CreateActivity.db.close(); 
	    	Intent i = new Intent(ExpensesActivity.this, AddexpenseActivity.class);
	    	startActivity(i);
	    	finish();}
	    	
	    	else{
	    		Toast.makeText(ExpensesActivity.this, "Event name cannot be blank", Toast.LENGTH_SHORT).show();
	    	}
			}
	     });
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

	    if (keyCode == KeyEvent.KEYCODE_BACK) {

	    	Intent i = new Intent(ExpensesActivity.this, CreateActivity.class);
	    	startActivity(i);
	        finish();
	        }
	       	return super.onKeyDown(keyCode, event);
	}
	
}
